package com.kyexpress.vms.component;

import com.intellij.notification.NotificationDisplayType;
import com.intellij.notification.NotificationGroup;
import com.intellij.openapi.module.ModuleManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.ui.MessageDialogBuilder;
import com.intellij.ui.EditorTextField;
import com.intellij.ui.table.JBTable;
import com.kyexpress.vms.enitity.DataCenter;
import com.kyexpress.vms.enitity.Dict;
import com.kyexpress.vms.enitity.MyConfig;
import com.kyexpress.vms.markdown.MDFreeMarkProcessor;
import com.kyexpress.vms.util.MyNotifier;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import java.awt.*;
import java.io.*;
import java.net.URL;

public class SynchronizeDictDialog extends DialogWrapper {
    
    private Project project;
    private static final NotificationGroup NOTIFICATION_GROUP =
            new NotificationGroup("PengW Notification Group", NotificationDisplayType.BALLOON, true);
    
    
    public SynchronizeDictDialog(Project project) {
        super(true);
        this.project = project;
        DataCenter.project=project;
        init();
        setTitle("同步dev字典到本地");
    }
    
    @Nullable
    @Override
    protected JComponent createCenterPanel() {
        JBTable jbTable = new JBTable();
        jbTable.setModel(DataCenter.TABLE_MODEL);
        jbTable.setEnabled(true);
        DataCenter.fill();
        JScrollPane jbScrollPane = new JScrollPane(jbTable);
        jbTable.getModel().addTableModelListener(new TableModelListener() {
            @Override
            public void tableChanged(TableModelEvent e) {
                if (e.getType() == 0) {
                    int firstRow = e.getFirstRow();
                    String valueAt = (String) DataCenter.TABLE_MODEL.getValueAt(firstRow, 0);
                    Dict dict = DataCenter.DICT_LIST.get(firstRow);
                    dict.setFieldName(valueAt);
                }
            }
        });
        return jbScrollPane;
    }
    
    @Override
    protected JComponent createSouthPanel() {
        JPanel panel = new JPanel(new FlowLayout());
        JLabel label = new JLabel("枚举类名");
        panel.add(label);
        EditorTextField jField = new EditorTextField();
        String fileName = DataCenter.getFileName();
        jField.setText(fileName);
        panel.add(jField);
        JButton btnAdd = new JButton("生成枚举");
        //按钮点击事件处理
        btnAdd.addActionListener(e -> {
            String newFileName = jField.getText();
            File file = new File(DataCenter.module.getModuleFile().getParent().getPath()+"/src/main/java/"+ MyConfig.getInstance().package_name.replace(".","/") +"/"+ newFileName + ".java");
            if (!file.getParentFile().exists()) {
                boolean yes = MessageDialogBuilder.yesNo("请检查确认", String.format("枚举类保存路径: %s 不存在,是否创建?",file.getParentFile().getPath())).isYes();
                if (yes) {
                    file.getParentFile().mkdirs();
                }
            }
            BufferedWriter bufferedWriter = null;
            try {
                boolean yes = true;
                if (file.exists()) {
                    yes = MessageDialogBuilder.yesNo("请检查确认", "该枚举类已存在,是否覆盖?").isYes();
                }
                if (yes) {
                    bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "utf-8"));
                    MDFreeMarkProcessor.process(bufferedWriter, newFileName);
                    MyNotifier.notifyInformation(this.project, "生成成功");
                    this.close(DialogWrapper.OK_EXIT_CODE);
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            } finally {
                if (bufferedWriter != null) {
                    try {
                        bufferedWriter.close();
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                }
            }
            
            
        });
        panel.add(btnAdd);
        return panel;
    }
    
    @Override
    public void doCancelAction() {
        super.doCancelAction();
        DataCenter.reset();
    }
    
    public static void main(String[] args) {
        String path = ClassLoader.getSystemResource("").getPath();
        URL resource = SynchronizeDictDialog.class.getClassLoader().getResource("");
        System.out.println(path);
        System.out.println(resource.getPath());
    }
}

